#include<bits/stdc++.h>
using namespace std;
const int MAXN=60,INF=0x7fffffff;
int n,m,head[MAXN*2],cnt,idx[MAXN*2],tot,sum,ans;
bool vis[MAXN];
struct edge
{
	int u,v,next,val;
}e[MAXN*MAXN*2+MAXN*4],temp[MAXN*MAXN*2+MAXN*4];
void addedge(int x,int y,int z)
{
	e[cnt]=(edge){x,y,head[x],z};
	head[x]=cnt++;
	return;
}
void input(int x)
{
	char ch=getchar();
	while(true)
	{
		int y=0;
		while(ch<'0'||ch>'9')
		{
			if(ch=='\n')return;
			ch=getchar();
		}
		while(ch>='0'&&ch<='9')
		{
			y=y*10+ch-'0';
			ch=getchar();
		}
		addedge(x,y+n,INF);
		addedge(y+n,x,0);
	}
}
void bfs()
{
	memset(idx,0,sizeof(idx));
	queue<int>q;
	q.push(0);
	idx[0]=1;
	while(!q.empty())
	{
		int u=q.front();q.pop();
		for(int i=head[u];~i;i=e[i].next)
		{
			int v=e[i].v;
			if(idx[v]||!e[i].val)continue;
			idx[v]=idx[u]+1;
			q.push(v);
		}
	}
	return;
}
int dfs(int u,int _min)
{
	if(u==n+m+1)return _min;
	int k,ret=0;
	for(int i=head[u];~i;i=e[i].next)
	{
		int v=e[i].v;
		if(idx[v]!=idx[u]+1||!e[i].val)continue;
		k=dfs(v,min(_min-ret,e[i].val));
		e[i].val-=k;
		e[i^1].val+=k;
		ret+=k;
		if(ret==_min)break;
	}
	return ret;
}
int dinic()
{
	int ret=0;
	while(true)
	{
		bfs();
		if(!idx[n+m+1])break;
		ret+=dfs(0,INF);
	}
	return ret;
}
bool check(int x)
{
	for(int i=head[x];~i;i=e[i].next)
	{
		int v=e[i].v;
		if(!v)continue;
		v-=n;
		if(!vis[v])return false;
	}
	return true;
}
int main()
{
	memset(head,-1,sizeof(head));
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&idx[i]);
		sum+=idx[i];
		input(i);
	}
	for(int i=1;i<=n;++i)
	{
		addedge(0,i,idx[i]);
		addedge(i,0,0);
	}
	tot=cnt;
	for(int i=1;i<=m;++i)
	{
		int x;
		scanf("%d",&x);
		addedge(i+n,n+m+1,x);
		addedge(n+m+1,i+n,0);
	}
	memcpy(temp,e,sizeof(e));
	ans=dinic();
	for(int i=tot;i<cnt;i+=2)
	{
		memcpy(e,temp,sizeof(temp));
		int rev=e[i].val;
		e[i].val=0;
		if(dinic()+rev==ans)vis[e[i].u-n]=true;
		e[i].val=rev;
	}
	for(int i=1;i<=n;++i)
		if(check(i))printf("%d ",i);
	puts("");
	for(int i=n+1;i<=n+m;++i)
		if(vis[i-n])printf("%d ",i-n);
	puts("");
	printf("%d\n",sum-ans);
	return 0;
}
